<template>
  <section>
    <!-- 工具栏 -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="condition">
        <el-form-item>
          <el-select
            v-model="condition.AR_number"
            placeholder="区域编号"
            @change="areaChanged(condition.AR_number)"
          >
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="condition.SSL_number" placeholder="宿舍楼编号">
            <el-option
              v-for="item in dorm"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="condition.RE_number" placeholder="物品编码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="condition.RE_name" placeholder="物品名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 操作栏 -->
    <el-col>
      <el-form :inline="true">
        <el-form-item>
          <el-button size="small" type="success" @click="newFormVisible = true">新增</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button size="small" type="success" @click="outExcel">导出EXCEL</el-button>
        </el-form-item>-->
      </el-form>
    </el-col>

    <!-- 数据展示 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="selsChange"
      v-loading="Loading"
    >
      <el-table-column align="center" type="selection" fixed width="50"></el-table-column>
      <el-table-column align="center" label="序号" fixed type="index" width="50"></el-table-column>
      <el-table-column align="center" label="区域名称" prop="AR_name" fixed min-width="150"></el-table-column>
      <el-table-column align="center" label="物品名称" prop="RE_name" min-width="100"></el-table-column>
      <el-table-column align="center" label="资产编码" prop="RE_number" min-width="100"></el-table-column>
      <el-table-column align="center" label="单价" prop="RE_price" min-width="60"></el-table-column>
      <el-table-column align="center" label="赔偿价格" prop="RE_compensation_price" min-width="70"></el-table-column>
      <el-table-column align="center" label="总数量" prop="RE_tnumber" min-width="60"></el-table-column>
      <el-table-column align="center" label="可用数量" prop="RE_available" min-width="80"></el-table-column>
      <el-table-column align="center" label="已领用数量" prop="RE_received" min-width="80"></el-table-column>
      <!-- <el-table-column align="center" label="未归还数量" prop="unReturn" min-width="80"></el-table-column> -->
      <el-table-column align="center" label="备注" prop="RE_remarks" min-width="100"></el-table-column>
      <el-table-column align="center" label="操作" min-width="150" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index,scope.row)">停用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="currentChange"
        :current-page="currentPage"
        :total="totalPage"
        style="float:right;"
      ></el-pagination>
    </el-col>

    <!-- 新增 -->
    <el-dialog title="新增" :visible.sync="newFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="newForm" :model="newGoods" :rules="rule">
        <el-form-item label="所属区域" prop="AR_number">
          <el-select
            v-model="newGoods.AR_number"
            placeholder="区域编号"
            @change="areaChange(newGoods.AR_number)"
          >
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属宿舍楼" prop="SSL_number">
          <el-select v-model="newGoods.SSL_number" placeholder="宿舍楼编号">
            <el-option
              v-for="item in dormd"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用品名称" prop="RE_name">
          <el-input v-model="newGoods.RE_name" placeholder="用品名称"></el-input>
        </el-form-item>
        <el-form-item label="资产编码" prop="RE_number">
          <el-input
            v-model="newGoods.RE_number"
            placeholder="资产编码"
            @blur="checkGoodsName(newGoods.RE_number,'new')"
          ></el-input>
        </el-form-item>
        <el-form-item label="单价" prop="RE_price">
          <el-input v-model="newGoods.RE_price" placeholder="单价"></el-input>
        </el-form-item>
        <el-form-item label="赔偿价格" prop="RE_compensation_price">
          <el-input v-model="newGoods.RE_compensation_price" placeholder="赔偿价格"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="RE_tnumber">
          <el-input v-model="newGoods.RE_tnumber" placeholder="数量"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="RE_remarks">
          <el-input v-model="newGoods.RE_remarks" placeholder="备注" type="textarea" autosize></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="newFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="newSubmit('newForm')" :loading="newLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 变更 -->
    <el-dialog title="编辑" :visible.sync="alterFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="alterForm" :model="alterGoods" :rules="rule">
        <el-form-item label="所属区域">
          <el-input v-model="alterGoods.AR_name" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="用品名称" prop="RE_name">
          <el-input v-model="alterGoods.RE_name" placeholder="用品名称"></el-input>
        </el-form-item>
        <el-form-item label="资产编码" prop="RE_number">
          <el-input v-model="alterGoods.RE_number" placeholder="资产编码" disabled></el-input>
        </el-form-item>
        <el-form-item label="单价" prop="RE_price">
          <el-input v-model="alterGoods.RE_price" placeholder="单价"></el-input>
        </el-form-item>
        <el-form-item label="赔偿价格" prop="RE_compensation_price">
          <el-input v-model="alterGoods.RE_compensation_price" placeholder="赔偿价格"></el-input>
        </el-form-item>
        <el-form-item label="总数量" prop="RE_tnumber">
          <el-input v-model="alterGoods.RE_tnumber" placeholder="总数量"></el-input>
        </el-form-item>
        <!-- <el-form-item label="未归还数量" prop="unReturn">
          <el-input v-model="alterGoods.unReturn" placeholder="未归还数量"></el-input>
        </el-form-item>-->
        <el-form-item label="备注" prop="RE_remarks">
          <el-input v-model="alterGoods.RE_remarks" placeholder="备注" type="textarea" autosize></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="alterFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click.native="alterSubmit('alterForm')"
          :loading="alterLoading"
        >提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { area_select } from "../../api/area.js";
import { building_select } from "../../api/floor.js";
import {
  receiving_items_select,
  receiving_items_add,
  receiving_items_update,
  receiving_items_delete,
} from "../../api/usegoods.js";
export default {
  data() {
    // 进行数字验证
    var checkNumber = (rule, value, callback) => {
      var re = /^[0-9]+.?[0-9]*$/;
      if (!value) {
        return callback(new Error("值不能为空"));
      }
      setTimeout(() => {
        if (!re.test(value)) {
          callback(new Error("请输入数字值"));
        } else {
          callback();
        }
      }, 100);
    };
    return {
      condition: {
        AR_number: null,
        SSL_number: null,
        RE_number: null,
        RE_name: null,
        limit: 1,
        page: 1,
      },
      tableData: [
        {
          AR_number: null,
          AR_name: null,
          RE_name: null,
          RE_number: null,
          RE_price: null,
          RE_compensation_price: null,
          RE_tnumber: null,
          RE_remarks: null,
        },
      ],
      newGoods: {
        AR_number: null,
        SSL_number: null,
        RE_name: null,
        RE_number: null,
        RE_price: null,
        RE_compensation_price: null,
        RE_tnumber: null,
        RE_remarks: null,
      },

      rule: {
        AR_number: [
          { required: true, message: "请选择宿舍楼区域", trigger: "change" },
        ],
        RE_name: [
          { required: true, message: "请输入物品名称", trigger: "blur" },
        ],
        RE_number: [
          { required: true, message: "请输入物品编码", trigger: "blur" },
        ],
        RE_price: [
          {
            validator: checkNumber,
            required: true,
            trigger: "blur",
          },
        ],
        RE_compensation_price: [
          {
            validator: checkNumber,
            required: true,
            trigger: "blur",
          },
        ],
        RE_tnumber: [
          {
            validator: checkNumber,
            required: true,
            trigger: "blur",
          },
        ],
        unReturn: [
          {
            validator: checkNumber,
            required: true,
            trigger: "blur",
          },
        ],
        RE_remarks: [{ required: false }],
      },
      alterGoods: {
        AR_number: null,
        SSL_number: null,
        RE_name: null,
        RE_number: null,
        RE_price: null,
        RE_compensation_price: null,
        RE_tnumber: null,
        RE_remarks: null,
      },
      area: [],
      dorm: [],
      dormd: [],
      newFormVisible: false,
      alterFormVisible: false,
      Loading: false,
      newLoading: false,
      alterLoading: false,
      currentPage: 1,
      totalPage: 1,
    };
  },
  methods: {
    getArea() {
      area_select({ AR_number: "", AR_name: "", page: 1 }).then((res) => {
        if (res) {
          for (let index = 0; index < res.data.length; index++) {
            this.area.push({
              label: res.data[index].AR_name,
              value: res.data[index].AR_number,
            });
          }
        }
      });
    },
    //主界面查询区域对应宿舍楼
    areaChanged() {
      if (this.condition.AR_number === "") {
        return true;
      }
      // console.log(this.condition._area);
      building_select({
        AR_number: this.condition.AR_number,
        SSL_number: "",
        SSL_admin: "",
        page: 1,
      }).then((res) => {
        if (res) {
          this.dorm = [];
          for (let index = 0; index < res.data.length; index++) {
            this.dorm.push({
              label: res.data[index].SSL_name,
              value: res.data[index].SSL_number,
            });
          }
        } else {
          this.dorm = [
            {
              value: "",
              label: "",
            },
          ];
          this.condition._dorm = "";
        }
      });
    },
    //新增界面查询区域对应宿舍楼
    areaChange() {
      if (this.newGoods.AR_number === "") {
        return true;
      }
      // console.log(this.condition._area);
      building_select({
        AR_number: this.newGoods.AR_number,
        SSL_number: "",
        SSL_admin: "",
        page: 1,
      }).then((res) => {
        if (res) {
          this.dormd = [];
          for (let index = 0; index < res.data.length; index++) {
            this.dormd.push({
              label: res.data[index].SSL_name,
              value: res.data[index].SSL_number,
            });
          }
        } else {
          this.dormd = [
            {
              value: "",
              label: "",
            },
          ];
          this.condition._dorm = "";
        }
      });
    },
    //查询
    getData() {
      this.Loading = true;
      receiving_items_select(this.condition).then((res) => {
        this.Loading = false;
        if (res) {
          this.totalPage = res.page;
          this.tableData = res.data;
        } else {
          this.tableData = null;
          this.totalPage = 0;
          this.currentPage = 0;
        }
      });
    },
    search() {
      this.condition.limit = 1;
      this.getData();
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    // 分页
    currentChange(val) {
      this.condition.page = val === undefined ? this.currentPage : val;
      this.getData();
    },
    // 新增
    newSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 数量不得为负
          if (this.newGoods.RE_tnumber < 0) {
            this.$message.error("数量不得未负");
            return false;
          }
          // console.log(this.newGoods);
          this.newLoading = true;
          receiving_items_add(this.newGoods).then((res) => {
            this.newLoading = false;
            if (res) {
              this.$message.success(res.msg);
              this.$refs[formName].resetFields();
              this.newFormVisible = false;
              this.getData();
            }
          });
        } else {
          this.$message.error("请将信息填写完整!");
          return false;
        }
      });
    },
    // 点击编辑:
    handleEdit(index, row) {
      this.alterFormVisible = true;
      this.alterGoods = Object.assign({}, row);
    },
    // 修改
    alterSubmit(forName) {
      this.$refs[forName].validate((valid) => {
        if (valid) {
          this.alterLoading = true;
          receiving_items_update(this.alterGoods).then((res) => {
            this.alterLoading = false;
            if (res) {
              this.$message.success(res.msg);
            }
            this.getData();
            this.alterFormVisible = false;
            return true;
          });
        } else {
          this.$message.error("请将信息填写完整!");
          return false;
        }
      });
    },
    // 点击删除
    handleDel(index, row) {
      this.$confirm("此操作将永久停用该物品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log(row);
          receiving_items_delete(
            row.AR_number,
            row.SSL_number,
            row.RE_number
          ).then((res) => {
            if (res) {
              this.$message({
                type: "success",
                message: "停用成功!",
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作",
          });
        });
    },
    checkGoodsName(goodsNumber, type) {
      // this.Loading = true;
      // checkNumberRepeat(goodsNumber, "use").then((res) => {
      //   this.Loading = false;
      //   if (!res) {
      //     // this.$message.error("编码重复!");
      //     if (type === "new") {
      //       this.newGoods.goodsNumber = "";
      //     } else if (type === "alter") {
      //       this.alterGoods.goodsNumber = "";
      //     }
      //     return false;
      //   } else {
      //     // this.initializeLoading = false;
      //     return true;
      //   }
      // });
    },
  },
  mounted() {
    this.getArea();
    this.getData();
  },
};
</script>